<?php


function form () {
// Array Position
$cellnames=array('A1','A2','A3','A4','A5','B1','B2','B3','B4','B5','C1','C2','C3','C4','C5','D1','D2','D3','D4','D5','E1','E2','E3','E4','E5','F1','F2','F3','F4','F5','E1','E2','E3','E4','E5','F1','F2','F3','F4','F5','G1','G2','G3','G4','G5');

// start session
session_start();

//get cells
$cells = file_get_contents('http://127.0.0.1/clearcard/auth.php?cmd=getcells');

$token = strtok($cells," ");
$c1 = (int)$token;

$token = strtok(" ");
$c2 = (int)$token;

$token = strtok(" ");
$c3 = (int)$token;


$_SESSION['c1'] = $c1;
$_SESSION['c2'] = $c2;
$_SESSION['c3'] = $c3;

//echo $cells."</br>---<br>";
//echo $c1." ".$c2." ".$c3."<br>";

//store cells

//present login page

echo "<center><form METHOD=GET action=login.php <br>Username<br><input type=text  name=uid><br><br><br><input type=hidden name=passwd><br>Please enter the Clearcard numbers associated with these coordinates.<br>".$cellnames[$c1-1]." ".$cellnames[$c2-1]." ".$cellnames[$c3-1]."<br><input type=text name=codes><br><sub>Example: 183294678901<br><br><input type=submit name=submit value=\"Submit\"><input type=hidden name=cmd value=authme></form></center>";     
}



function authenticate ($uid,$passwd,$codes) {
//echo "In authentication function";

// do ldap auth
// Future demo

// open session
session_start();

//call auth.php, pass uid, cells, and codes
$result = file_get_contents('http://127.0.0.1/clearcard/auth.php?cmd=auth&uid='.$uid.'&passwd='.$passwd.'&codes='.$codes.'&c1='.$_SESSION['c1'].'&c2='.$_SESSION['c2'].'&c3='.$_SESSION['c3']);

echo "Your login was a $result";



}


switch ($_GET['cmd']) {
        case "authme": 
                authenticate($_GET['uid'],$_GET['passwd'],$_GET['codes']);
                break;
        default:
                form();
}

?>
